package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.common.PageBean;
import com.itheima.common.R;
import com.itheima.dto.StudentQueryDto;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/students")
public class StudentController {
    @Autowired
    private StudentService studentService;

    @GetMapping
    public R pageQuery(StudentQueryDto dto) {
        log.info("分页条件查询学员：{}", dto);
        PageBean pageBean = studentService.pageQuery(dto);
        return R.ok(pageBean);
    }

    @DeleteMapping("/{ids}")
    @Log
    public R delete(@PathVariable List<Integer> ids) {
        log.info("批量删除学员：{}", ids);
        studentService.delete(ids);
        return R.ok();
    }

    @PostMapping
    @Log
    public R insert(@RequestBody Student student) {
        log.info("新增学员：{}", student);
        studentService.insert(student);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R findById(@PathVariable Integer id) {
        log.info("根据ID查询学员：{}", id);
        Student student = studentService.findById(id);
        return R.ok(student);
    }

    @PutMapping("/violation/{id}/{score}")
    @Log
    public R violationHandle(@PathVariable Integer id, @PathVariable Integer score) {
        log.info("处理学员违纪：{}, {}", id, score);
        studentService.violationHandle(id, score);
        return R.ok();
    }

    @PutMapping
    @Log
    public R update(@RequestBody Student student) {
        log.info("更新学员：{}", student);
        studentService.update(student);
        return R.ok();
    }

}
